package net.hn.hnms.biz.process.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
//import net.hn.hnms.biz.common.activiti.result.ProcessHistoryResult;
//import net.hn.hnms.biz.common.activiti.result.ProcessTaskResult;
import net.hn.hnms.biz.process.domain.ShiftTurnoverPo;
import net.hn.hnms.biz.process.domain.WorkTicketPo;
import net.hn.hnms.biz.process.domain.dto.ApproveDto;
import net.hn.hnms.biz.process.domain.dto.FromUpdateDto;
import net.hn.hnms.biz.process.domain.dto.WorkTicketFlowDataDto;
import net.hn.hnms.biz.process.domain.dto.WorkTicketFlowDto;
import net.hn.hnms.biz.process.domain.vo.ApproveVo;
import net.hn.hnms.biz.process.service.WorkTicketFlowService;
import net.hn.hnms.biz.process.service.WorkTicketService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.log.annotation.Log;
import net.hn.hnms.sys.common.log.enums.BusinessType;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.mybatis.core.page.TableDataInfo;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import net.hn.hnms.sys.system.api.model.UserVo;
//import org.activiti.engine.ProcessEngine;
//import org.activiti.engine.ProcessEngines;
//import org.activiti.engine.RuntimeService;
//import org.activiti.engine.TaskService;
//import org.activiti.engine.task.Task;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述: 工作票管理
 *
 * @param
 * @auther: WangLong
 * @date: 2022/7/18 10:53
 * @return
 */
//ApiIgnore暂时放置
//@ApiIgnore
@Api(value = "工作票管理控制器", tags = {"工作票管理"})
@RestController
@RequestMapping("workTicket")
public class WorkTicketController {

    @Resource
    private WorkTicketService workTicketService;
    @Resource
    private WorkTicketFlowService workTicketFlowService;

//    @Resource
//    private RuntimeService runtimeService;

    /**
     * 功能描述:分页条件查询工作票列表
     *
     * @param
     * @return net.hn.hnms.sys.common.core.domain.R
     * @auther: WangLong
     * @date: 2022/7/22 14:06
     */

    @ApiOperation("分页条件查询工作票列表")
    @GetMapping("ticket/pageList")
    @AutoSetMineCode
    @Log(title = "查询工作票列表", businessType = BusinessType.OTHER)
    public R<ResponsePage<WorkTicketPo>> ticketPageList(PageQuery pageQuery, WorkTicketPo workTicketPo) {

        TableDataInfo<WorkTicketPo> page = workTicketService.ticketPageList(pageQuery, workTicketPo);
        ResponsePage responsePage = new ResponsePage<ShiftTurnoverPo>(page.getRows(), page.getTotal());

        return R.ok(responsePage);
    }


    /**
     * 功能描述: 添加工作票
     *
     * @param
     * @return net.hn.hnms.sys.common.core.domain.R
     * @auther: WangLong
     * @date: 2022/7/22 14:09
     */
    @ApiOperation("添加工作票")
    @PostMapping("ticket/add")
    @Log(title = "添加工作票", businessType = BusinessType.INSERT)
    public R ticketAdd(@Validated @RequestBody WorkTicketPo workTicketPo) {
        boolean save = workTicketService.ticketAdd(workTicketPo);
        if (save) {
            return R.ok("保存工作票成功");
        }

        return R.fail("保存工作票失败");
    }

    @ApiOperation("工作票修改")
    @PutMapping("ticket/update")
    @Log(title = "修改工作票", businessType = BusinessType.UPDATE)
    public R update(@Validated @RequestBody WorkTicketPo workTicketPo) {
        boolean is = workTicketService.updateById(workTicketPo);
        if (is) {
            return R.ok("更新工作票成功");
        }

        return R.fail("更新工作票失败");
    }

    /**
     * 功能描述: 工作票批量删除
     *
     * @param
     * @return net.hn.hnms.sys.common.core.domain.R
     * @auther: WangLong
     * @date: 2022/7/22 14:10
     */
    @ApiOperation("工作票批量删除")
    @DeleteMapping("ticket/delete")
    @Log(title = "删除工作票", businessType = BusinessType.DELETE)
    public R delete(@RequestBody List<String> idList) {
        boolean b = workTicketService.removeByIds(idList);
        if (b) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    /**
     * 功能描述: 工作票状态修改
     *
     * @param
     * @return net.hn.hnms.sys.common.core.domain.R
     * @auther: WangLong
     * @date: 2022/7/22 14:13
     */

    @ApiOperation("工作票状态修改")
    @PutMapping("ticket/amendStatus/{id}")
    @Log(title = "修改工作票状态", businessType = BusinessType.UPDATE)
    public R amendStatusById(@PathVariable String id) {
        Boolean is = workTicketService.amendStatusById(id);
        if (is) {
            return R.ok("工作票状态修改修改成功");
        }
        return R.fail("工作票状态修改修改失败");
    }

    /**
     * 功能描述: 工作票设置操作或设置审批流程
     *
     * @param
     * @return net.hn.hnms.sys.common.core.domain.R
     * @auther: WangLong
     * @date: 2022/7/22 14:18
     */

    //版本1 宽表形式
//    @ApiOperation("工作票审批流程设置")
//    @PostMapping("addApprove/update")
//    public R addApprove(@Validated @RequestBody WorkTicketDto workTicketDto) {
//       Boolean is= workTicketFlowService.addApproveUpdate(workTicketDto);
//        if (is) {
//
//            return R.ok("添加工作票审批成功");
//        }
//        return R.fail("添加工作票审批失败");
//    }
    //版本2 json方式
    @ApiOperation("工作票审批流程设置")
    @PostMapping("addApprove/update")
    @Log(title = "修改工作票审批流程", businessType = BusinessType.OTHER)
    public R addApproveUpdate(@Validated @RequestBody WorkTicketFlowDto workTicketFlowDto) {
        Boolean is = workTicketFlowService.addApproveUpdate(workTicketFlowDto);
        if (is) {

            return R.ok("添加工作票审批成功");
        }
        return R.fail("添加工作票审批失败");
    }

    @ApiOperation("From表单修改")
    @PostMapping("from/update")
    @Log(title = "修改工作票FORM表单", businessType = BusinessType.UPDATE)
    public R fromUpdate(@RequestBody FromUpdateDto fromUpdateDto) {

        Boolean is = workTicketFlowService.fromUpdate(fromUpdateDto);
        if (is) {

            return R.ok("修改表单成功");
        }
        return R.fail("修改表单失败");
    }

    /**
     * 功能描述: 获取工作票审批列表
     *
     * @param pageQuery
     * @return net.hn.hnms.sys.common.mybatis.core.page.TableDataInfo<net.hn.hnms.biz.process.domain.vo.WorkTicketFlowOneVo>
     * @auther: WangLong
     * @date: 2022/8/11 9:52
     */

    @ApiOperation("获取审批管理列表")
    @GetMapping("approve/pageList")
    @AutoSetMineCode
    @Log(title = "查询审批管理列表", businessType = BusinessType.OTHER)
    public R<ResponsePage<List<ApproveVo>>> approvePageList(PageQuery pageQuery, ApproveDto approveDto) {
        ResponsePage<List<ApproveVo>> page = workTicketFlowService.approvePageList(pageQuery, approveDto);
        return R.ok(page);
    }

    @ApiOperation("审批批量删除")
    @DeleteMapping("approve/delete")
    @Log(title = "删除审批", businessType = BusinessType.DELETE)
    public R approveDelete(@RequestBody List<String> idList) {
        Boolean is = workTicketFlowService.approveDelete(idList);
        if (is) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    /**
     * 功能描述: 发起审批流程
     *
     * @param workTicketFlowDto
     * @return net.hn.hnms.sys.common.core.domain.R
     * @auther: WangLong
     * @date: 2022/8/11 17:34
     */

    @ApiOperation("发起审批流程")
    @PutMapping("startApprove")
    @Log(title = "添加审批流程", businessType = BusinessType.INSERT)
    public R startApprove(@Validated @RequestBody WorkTicketFlowDto workTicketFlowDto) {
        Boolean is = workTicketFlowService.startApprove(workTicketFlowDto);
        if (is) {
            return R.ok("发起审批流程成功");
        }
        return R.fail("发起审批流程失败");
    }

    @ApiOperation("完成审批")
    @GetMapping("accomplishApprove")
    @Log(title = "完成审批流程", businessType = BusinessType.INSERT)
    public R accomplishApprove(@ApiParam(value = "请输入审批工作票ID") @RequestParam(value = "businessKey") String businessKey,
                               @ApiParam(value = "是否需要登录人审批，默认为true") @RequestParam(value = "is", required = false, defaultValue = "true") Boolean is,
                               @ApiParam(value = "是否同意审批，1同意，0拒绝") @RequestParam(value = "approve") String approve,
                               @ApiParam(value = "审批意见内容") @RequestParam(value = "content") String content) {
        Boolean aBooleanis = workTicketFlowService.accomplishApprove(businessKey, is,approve,content);

        return R.ok(aBooleanis);
    }

    @ApiOperation("获取我的审批列表")
    @GetMapping("myApprove/pageList")
    @AutoSetMineCode
    @Log(title = "查询我的审批列表", businessType = BusinessType.OTHER)
    public R<List<ApproveVo>> myApprove(@ApiParam(value = "煤矿编码") String mineCode) {

        List<ApproveVo> page = workTicketFlowService.myApprove(mineCode);
        return R.ok(page);
    }

    /**
     * 功能描述: 根据工作票ID获取审批列表
     *
     * @param id
     * @return net.hn.hnms.sys.common.core.domain.R
     * @auther: WangLong
     * @date: 2022/8/11 17:34
     */

    @ApiOperation("根据工作票ID获取审批序号列表")
    @PutMapping("approve/{id}")
    @Log(title = "工作票ID获取审批序号列表", businessType = BusinessType.OTHER)
    public R<WorkTicketFlowDto> approveById(@PathVariable("id") String id) {

        WorkTicketFlowDto workTicketFlowDto = workTicketFlowService.approveById(id);
        return R.ok(workTicketFlowDto);
    }


    /**
     * 功能描述: 撤销审批
     *
     * @param id
     * @return net.hn.hnms.sys.common.core.domain.R
     * @auther: WangLong
     * @date: 2022/8/11 17:34
     */

    @ApiOperation("撤销审批")
    @PutMapping("revocationApprove/{id}")
    @Log(title = "撤销审批", businessType = BusinessType.UPDATE)
    public R revocationApproveById(@PathVariable("id") String id) {

        Boolean is = workTicketFlowService.revocationApproveById(id);
        if (is) {
            return R.ok("撤销成功");
        }
        return R.fail("撤销失败");
    }



    /**
     * 功能描述: 根据工作票DataID获取审批序号列表
     *
     * @param id
     * @return net.hn.hnms.sys.common.core.domain.R
     * @auther: WangLong
     * @date: 2022/8/11 17:34
     */

    @ApiOperation("根据工作票DataID获取审批序号列表")
    @PutMapping("approveData/{id}")
    @Log(title = "工作票DataID获取审批序号列表", businessType = BusinessType.OTHER)
    public R<WorkTicketFlowDataDto> approveDataById(@PathVariable("id") String id) {

        WorkTicketFlowDataDto workTicketFlowDataDto = workTicketFlowService.approveDataById(id);
        return R.ok(workTicketFlowDataDto);
    }

//
//    @ApiOperation("后端测试专用--工作流--查询当前任务列表")
//    @PutMapping("task/{businessKey}")
//    public R<List<ProcessHistoryResult>> task(@PathVariable("businessKey") String businessKey) {
//
//        List<ProcessHistoryResult> processHistoryResults = workTicketFlowService.taskByid(businessKey);
//        return R.ok(processHistoryResults);
//    }
//
//    @ApiOperation("后端测试专用--工作流--开启流程审批(默认同意第一条审批)")
//    @PutMapping("startProcess")
//    public R<ProcessTaskResult> startProcess(@RequestParam(value = "businessKey") String businessKey,
//                                             @RequestParam(value = "name") String name,
//                                             @RequestParam(value = "user") String user) {
//
//        ProcessTaskResult taskResult = workTicketFlowService.startProcess(businessKey, name, user);
//        return R.ok(taskResult);
//    }
//
//    @ApiOperation("后端测试专用--工作流--完成任务")
//    @PutMapping("executeProcessTask")
//    public R<ProcessTaskResult> executeProcessTask(@RequestParam(value = "businessKey") String businessKey,
//                                                   @RequestParam(value = "approve") Integer approve,
//                                                   @RequestParam(value = "content") String content,
//                                                   @RequestParam(value = "user") String user) {
//
//        ProcessTaskResult variables2 = workTicketFlowService.executeProcessTask(businessKey, approve, content, user);
//        return R.ok(variables2);
//    }
//
//    @ApiOperation("后端测试专用--工作流--输出base64")
//    @PutMapping("base")
//    public R<ProcessTaskResult> base(@RequestParam(value = "businessKey") String businessKey) {
//
//        String base64 = workTicketFlowService.base(businessKey);
//        return R.ok(base64);
//    }
//
//    @ApiOperation("后端测试专用--工作流--撤销审批")
//    @PutMapping("revocation")
//    public R revocation(@RequestParam(value = "businessKey") String businessKey) {
//         workTicketFlowService.revocation(businessKey);
//        return R.ok();
//    }
//    @ApiOperation("后端测试专用--工作流--删除审批")
//    @PutMapping("delete")
//    public R delete(@RequestParam(value = "businessKey") String businessKey) {
//        workTicketFlowService.delete(businessKey);
//        return R.ok();
//    }
//    @ApiOperation("后端测试专用--工作流--暂停运行的流程实例")
//    @PutMapping("suspend")
//    public R suspend(@RequestParam(value = "businessKey") String businessKey) {
//        workTicketFlowService.suspend(businessKey);
//        return R.ok();
//    }
//    @ApiOperation("后端测试专用--工作流--激活暂停的流程实例")
//    @PutMapping("activate")
//    public R activate(@RequestParam(value = "businessKey") String businessKey) {
//        workTicketFlowService.activate(businessKey);
//        return R.ok();
//    }
//
//    @ApiOperation("后端测试专用--工作流--判断当前人任务列表")
//    @PutMapping("taskList")
//    public R taskList(@RequestParam(value = "businessKey") String businessKey,
//                      @RequestParam(value = "name") String name) {
//        //        任务负责人
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
////        创建TaskService
//        TaskService taskService = processEngine.getTaskService();
////        根据流程key 和 任务负责人 查询任务
//        List<Task> list = taskService.createTaskQuery()
//                .processDefinitionKey(businessKey) //流程Key
//                .taskAssignee(name)//只查询该任务负责人的任务
//                .list();
//        for (Task task : list) {
//            System.out.println("流程实例id：" + task.getProcessInstanceId());
//            System.out.println("任务id：" + task.getId());
//            System.out.println("任务负责人：" + task.getAssignee());
//            System.out.println("任务名称：" + task.getName());
//        }
//        return R.ok(list);
//    }


    @ApiOperation("审批人员-批量新增审批人员")
    @PutMapping("person/add")
    @AutoSetMineCode
    @Log(title = "添加审批人员", businessType = BusinessType.INSERT)
    public R personAdd(@RequestBody()  List<Long> idList,@ApiParam(value = "煤矿编码") String mineCode) {
        Boolean is=workTicketFlowService.personAdd(idList,mineCode);
        if (is) {
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("审批人员-已添加审批人员列表")
    @GetMapping("person/trueApprovalList")
    @AutoSetMineCode
    @Log(title = "查询已添加审批人员列表", businessType = BusinessType.OTHER)
    public R<List<UserVo>> personTrueApprovalList(@ApiParam(value = "煤矿编码") String mineCode) {
        List<UserVo> list=workTicketFlowService.personTrueApprovalList(mineCode);
        return R.ok(list);
    }
    @ApiOperation("审批人员-未添加审批人员列表")
    @GetMapping("person/falseApprovalList")
    @AutoSetMineCode
    @Log(title = "查询未添加审批人员列表", businessType = BusinessType.OTHER)
    public R<List<UserVo>> personFalseApprovalList(@ApiParam(value = "煤矿编码") String mineCode) {
        List<UserVo> list=workTicketFlowService.personFalseApprovalList(mineCode);
        return R.ok(list);
    }
    @ApiOperation("审批人员-批量删除审批人员")
    @DeleteMapping("person/delete")
    @AutoSetMineCode
    @Log(title = "删除审批人员", businessType = BusinessType.DELETE)
    public R personDelete(@RequestBody()  List<Long> idList,@ApiParam(value = "煤矿编码") String mineCode) {
        Boolean is=workTicketFlowService.personDelete(idList,mineCode);
        if (is) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
    @ApiOperation("审批人员-根据用户id设置审批图片")
    @GetMapping("person/setSignaturePicture")
    @AutoSetMineCode
//    @Log(title = "更新审批人员信息", businessType = BusinessType.UPDATE)
    public R personSetSignaturePicture(@ApiParam(value = "煤矿编码")  String mineCode,
                                       @ApiParam(value = "用户ID")  Long userId,
                                       @ApiParam(value = "签名图片URL")  String url) {
        Boolean is=workTicketFlowService.personSetSignaturePicture(userId,mineCode,url);
        if (is) {
            return R.ok("更新成功");
        }
        return R.fail("更新失败");
    }
    @ApiOperation("审批人员-根据用户id获取审批图片url")
    @GetMapping("person/getSignaturePicture")
    @AutoSetMineCode
    public R<String> getSignaturePicture(@ApiParam(value = "煤矿编码") String mineCode,
                                       @ApiParam(value = "用户ID，如果为空默认查询当前登录人") Long userId) {
        String url=workTicketFlowService.getSignaturePicture(userId,mineCode);
        return R.ok("",url);
    }

}
